package com.sui.bigdata.rtcadmin.repository.model;

import com.sui.bigdata.rtcadmin.dto.JobConfigDto;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.Alias;

import java.util.Date;

/**
 * @author YongChen
 * @date 2019/11/6 14:41
 * @description
 * @email yong_chen@sui.com
 */
@Data
@Alias("JobConfig")
@Accessors(chain = true)
public class JobConfig {

    private long id;
    private String jobName;
    private String appId;
    private String execSql;
    private String developer;
    private String alarmGroupPhone;
    private String jobId;
    private int parallelism;
    private String queue;
    private int jobManagerMemory;
    private int taskManagerMemory;
    private int slots;
    private String status;
    private String webUrl;
    private String extendParam;
    private String description;
    private Date startTime;
    private Date createTime;
    private Date updateTime;
    private int isOnline;

    public static JobConfig buildJobConfig(JobConfigDto jobConfigDto){
        if (jobConfigDto == null){
            return null;
        }
        return new JobConfig()
                .setJobName(jobConfigDto.getJobName())
                .setDeveloper(jobConfigDto.getDeveloper())
                .setAlarmGroupPhone(jobConfigDto.getAlarmGroupPhone() == null ? "":jobConfigDto.getAlarmGroupPhone())
                .setExecSql(jobConfigDto.getExecSql())
                .setParallelism(jobConfigDto.getParallelism() <= 0 ? 1:jobConfigDto.getParallelism() )
                .setQueue(StringUtils.isNotBlank(jobConfigDto.getQueue()) ? jobConfigDto.getQueue() : "root.default")
                .setJobManagerMemory(jobConfigDto.getJobManagerMemory() <= 0 ? 768 : jobConfigDto.getJobManagerMemory())
                .setTaskManagerMemory(jobConfigDto.getTaskManagerMemory() <= 0 ? 768 : jobConfigDto.getTaskManagerMemory())
                .setSlots(jobConfigDto.getSlots() <= 0 ? 1 : jobConfigDto.getSlots())
                .setExtendParam(jobConfigDto.getExtendParam() == null ? "":jobConfigDto.getExtendParam())
                .setStatus(jobConfigDto.getStatus())
                .setWebUrl(jobConfigDto.getWebUrl())
                .setDescription(jobConfigDto.getDescription() == null ? "":jobConfigDto.getDescription())
                .setIsOnline(jobConfigDto.getIsOnline());
    }
}
